package com.acxy.ants.core.config;

import com.acxy.ants.core.Interceptor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-04-27
 */
final public class Interceptors {

    private final static List<Interceptor> interceptorList = new ArrayList<Interceptor>();

    public Interceptors add(Interceptor interceptor) {
        if (interceptor == null) {
            throw new IllegalArgumentException("interceptor can not be null");
        }
        interceptorList.add(interceptor);
        return this;
    }

    public static List<Interceptor> getInterceptorList() {
        return interceptorList;
    }

    /**
     * 在对象列表中查找类型一样的对象
     *
     * @param clsType 对象类型
     * @param <T>
     * @return
     */
    public static <T> T getClassObject(Class<T> clsType) {
        for (Object obj : interceptorList) {
            if (obj.getClass() == clsType) {
                return (T) obj;
            }
        }
        return null;
    }
}
